package com.tchy.cloud.controller;

import com.tchy.cloud.entity.ACommentEntity;
import com.tchy.cloud.entity.ASaysayEntity;
import com.tchy.cloud.entity.ATopicEntity;
import org.jboss.logging.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MutiLangUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 话题管理
 * Created by zjy on 2015/9/22.
 */
@Controller
@RequestMapping("/topicController")
public class ATopicController {

    private static final Logger logger = Logger.getLogger(ATopicController.class);

    @Autowired
    private SystemService systemService;

    private String message;


    /**
     * 话题管理列表页
     * @return
     */
    @RequestMapping(params = "topicList")
    public ModelAndView topicList(){
        return new ModelAndView("clound/articleManager/topic/topicList");
    }


    /**
     * 话题管理列表
     * @param topicEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(ATopicEntity topicEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){

        CriteriaQuery cq = new CriteriaQuery(ATopicEntity.class,dataGrid);

        cq.eq("deleted",0);

        cq.add();
        // 查询条件组装
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, topicEntity, request.getParameterMap());
        this.systemService.getDataGridReturn(cq,true);

        TagUtil.datagrid(response, dataGrid);
    }


    /**
     * 跳转到修改转视图
     * @return
     */
    @RequestMapping(params = "addorupdate")
    public ModelAndView addorupdate(ATopicEntity topicEntity,
                                    HttpServletRequest req) {


        if (topicEntity.getTopicId() != null) {
            topicEntity = systemService.getEntity(ATopicEntity.class, topicEntity.getTopicId());

            req.setAttribute("topicEntity", topicEntity);

        }


        return new ModelAndView("clound/articleManager/topic/topic");
    }





    /**
     * 添加话题管理
     *
     * @param topicEntity
     * @return
     */
    @RequestMapping(params = "save")
    @ResponseBody
    public AjaxJson save(ATopicEntity topicEntity, HttpServletRequest request) {

        AjaxJson j = new AjaxJson();
        if (topicEntity.getTopicId() != null) {

            try {

                ATopicEntity c = systemService.get(ATopicEntity.class, topicEntity.getTopicId());


                MyBeanUtils.copyBeanNotNull2Bean(topicEntity, c);

                systemService.saveOrUpdate(c);

                message = MutiLangUtil.paramUpdSuccess("话题管理["+topicEntity.getName()+"]");

            } catch (Exception e) {
                e.printStackTrace();
                message = MutiLangUtil.paramUpdFail("话题管理["+topicEntity.getName()+"]");
            }
            systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        } else {

            try {

                topicEntity.setChatNum(0);
                topicEntity.setCommentNum(0);
                topicEntity.setFollowNum(0);
                topicEntity.setCreateTime(new Date());
                topicEntity.setDeleted(0);
                systemService.save(topicEntity);

                message = MutiLangUtil.paramAddSuccess("话题管理["+topicEntity.getName()+"]");
            } catch (Exception e) {
                e.printStackTrace();

                message = "保存话题管理["+topicEntity.getName()+"]失败";
            }

            systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
        }
        j.setMsg(message);
        return j;
    }



    /**
     * 删除话题管理（逻辑删除）
     * @param id
     * @return
     */
    @RequestMapping(params = "del")
    @ResponseBody
    public AjaxJson del(String id){
        AjaxJson j = new AjaxJson();
        try {

            ATopicEntity topicEntity = systemService.get(ATopicEntity.class,Long.valueOf(id));

            if(topicEntity != null){

                topicEntity.setDeleted(1);
                systemService.saveOrUpdate(topicEntity);
                message = "删除话题["+topicEntity.getName()+"]成功";
            }else{
                message = "话题信息不存在，请刷新重试！";
            }


        } catch (Exception e) {

            message = "删除话题管理失败";
        }

        systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

        j.setMsg(message);
        return j;
    }

    /**
     * 话题管理列表页
     * @return
     */
    @RequestMapping(params = "saysayList")
    public ModelAndView saysayList(HttpServletRequest request,String topicId){

        request.setAttribute("topicId",topicId);
        return new ModelAndView("clound/articleManager/topic/saysay/saysayList");
    }


    /**
     * 话题管理列表
     * @param saysayEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "saysayDatagrid")
    public void saysayDatagrid(ASaysayEntity saysayEntity, Long topicId,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){

        CriteriaQuery cq = new CriteriaQuery(ASaysayEntity.class,dataGrid);

//        cq.eq("topic.topicId",topicId);
        cq.add();

        // 查询条件组装
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, saysayEntity, request.getParameterMap());
        this.systemService.getDataGridReturn(cq,true);

        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 回复详情列表页
     * @return
     */
    @RequestMapping(params = "parentCommentList")
    public ModelAndView parentCommentList(HttpServletRequest request,String commentId){

        request.setAttribute("commentId",commentId);
        return new ModelAndView("clound/articleManager/questions/comment/parentCommentList");
    }


    /**
     * 回复详情列表
     * @param commentEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "parentCommentDatagrid")
    public void parentCommentDatagrid(ACommentEntity commentEntity, Long commId, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){

        CriteriaQuery cq = new CriteriaQuery(ACommentEntity.class,dataGrid);

//        cq.eq("type",2);
        cq.eq("parent",commId);
        cq.add();
        // 查询条件组装
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, commentEntity, request.getParameterMap());
        this.systemService.getDataGridReturn(cq,true);

        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 删除话题管理（逻辑删除）
     * @param id
     * @return
     */
    @RequestMapping(params = "parentCommentDel")
    @ResponseBody
    public AjaxJson parentCommentDel(String id){
        AjaxJson j = new AjaxJson();
        try {

            ACommentEntity commentEntity = systemService.get(ACommentEntity.class,Long.valueOf(id));

            if(commentEntity != null){

                systemService.delete(commentEntity);
                message = "删除回复成功";
            }else{
                message = "回复信息不存在，请刷新重试！";
            }


        } catch (Exception e) {

            message = "删除回复失败";
        }

        systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

        j.setMsg(message);
        return j;
    }

}
